const http = require("http");

const port = 3000;

const app = http.createServer((req, res) => {

  res.setHeader("Access-Control-Allow-Origin", "*"); // * 允许所有
  res.setHeader(
    "Access-Control-Allow-Headers",
    "X-Requested-With,Origin,Content-Type,Accept,Authorization"
  );
  res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.setHeader("Access-Control-Allow-Credentials", "true");

  if (req.url === "/get") {
    res.setHeader("Content-Type", "application/json");
    res.end(JSON.stringify({ msg: "server is on" }));
  }
  if (req.url === "/post") {
    res.setHeader("Content-Type", "multipart/form-data");
    let body = "";
    req.on("data", (chunk) => {
      body += chunk;
    });
    req.on("end", () => {
      console.log(body);
      res.statusCode = 200;
      res.end(body);
    });
  }
});

app.listen(port, () => {
  console.log("http://localhost:" + port);
});
